package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.printeverywhere.delegator.CategoryManagerServiceDelegator;
import com.printeverywhere.dto.GeneralCategories;
import com.printeverywhere.util.AdminConstants;


public class ShowDynamicTreeAction extends ActionSupport implements ServletRequestAware{

	private HttpServletRequest request;
	private Map<Integer, GeneralCategories> treeMap=new HashMap<Integer, GeneralCategories>();
	private List<GeneralCategories> tempGeneralCategoriesList = new ArrayList<GeneralCategories>();



    public List<GeneralCategories> getTempGeneralCategoriesList() {
		return tempGeneralCategoriesList;
	}
	public void setTempGeneralCategoriesList(
			List<GeneralCategories> tempGeneralCategoriesList) {
		this.tempGeneralCategoriesList = tempGeneralCategoriesList;
	}
	@Override
    public String execute() throws Exception {
    	tempGeneralCategoriesList = CategoryManagerServiceDelegator.getParentList();
    	for (Iterator iterator = tempGeneralCategoriesList.iterator(); iterator.hasNext();) {
			GeneralCategories category = (GeneralCategories) iterator.next();
			category = CategoryManagerServiceDelegator.removeParentfromSet(category);
			treeMap.put(category.getCategoryId(), category);
		}
    	return SUCCESS;
    }
    public GeneralCategories getGeneralCategoriesFromCategoryId(int categoryid){
		 try{
		 tempGeneralCategoriesList = (List<GeneralCategories>) ActionContext.getContext().getApplication().get(AdminConstants.GENERALCATEGORIES);
			GeneralCategories type = new GeneralCategories();
			for (Iterator<GeneralCategories> iterator = tempGeneralCategoriesList.iterator(); iterator.hasNext();) {
				type = (GeneralCategories) iterator.next();
				if(categoryid == type.getCategoryId()){
					return type;
				}
			}
		 }catch (Exception e) {
			 addActionError(e.getMessage());
		}
			return null;
	 }
	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
	}
	public Map<Integer, GeneralCategories> getTreeMap() {
		return treeMap;
	}
	public void setTreeMap(Map<Integer, GeneralCategories> treeMap) {
		this.treeMap = treeMap;
	}
}

